<?php
/**
 * Short description for lil_blogs_attachments_plugin_controller.php
 *
 * Long description for lil_blogs_attachments_plugin_controller.php
 *
 * PHP versions 4 and 5
 *
 * Copyright (c) 2009, Miha Nahtigal
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright (c) 2009, Miha Nahtigal
 * @link          http://www.nahtigal.com/
 * @package       lil_blogs_attachments
 * @subpackage    lil_blogs_attachments
 * @since         v 1.0
 * @version       $Revision: 184 $
 * @modifiedby    $LastChangedBy: miha@nahtigal.com $
 * @lastmodified  $Date: 2009-10-21 20:52:07 +0200 (sre, 21 okt 2009) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
App::import('Controller', 'LilBlogs.LilBlogsPlugin');
/**
 * LilBlogsAttachmentsPlugin class
 *
 * @uses          LilBlogsPluginController
 * @package       lil_blogs
 * @subpackage    lil_blogs_attachments
 */
class LilBlogsAttachmentsPluginController extends LilBlogsPluginController {
/**
 * name property
 *
 * @var string 'LilBlogsAttachmentsPlugin'
 * @access public
 */
	var $name = 'LilBlogsAttachmentsPlugin';

/**
 * handlers property
 *
 * @var array
 * @access public
 */
	var $handlers = array(
		'before_construct_model' => array('function' => '_associateAttachments', 'params' => array()),
		'after_save_model' => array('function' => '_deleteAttachments', 'params' => array()),
		'before_save_post' => array('function' => '_beforePost', 'params' => array()),
		
		'form_edit_post' => array('function' => '_addAttachmentsPanel', 'params' => array()),
		'form_add_post' => array('function' => '_addAttachmentsPanel', 'params' => array()),
	);
	
	function _associateAttachments($model) {
		if ($model->name == 'Post') {
			$model->hasMany['LilBlogsAttachment'] = array(
				'className' => 'LilBlogsAttachments.LilBlogsAttachment',
				'foreignKey' => 'foreign_id',
				'conditions' => array('LilBlogsAttachment.model' => 'Post')
			);
		}
	}
	
	function _beforePost($controller, $data) {
		if (!empty($data['data']['LilBlogsAttachment'])) {
			foreach ((array)$data['data']['LilBlogsAttachment'] as $k => $v) {
				if (empty($v['filename']['name'])) unset($data['data']['LilBlogsAttachment'][$k]);
			}
		}
		return $data;
	}
	
	function _deleteAttachments($model) {
		if ($model->name == 'Post' && !empty($model->data['Post']['attachments_to_delete'])) {
			$a = ClassRegistry::init('LilBlogsAttachments.LilBlogsAttachment');
			$a->deleteAll(array('LilBlogsAttachment.id' => $model->data['Post']['attachments_to_delete']));
		}
	}

	function _addAttachmentsPanel($view, $form) {
		$a = ClassRegistry::init('LilBlogsAttachments.LilBlogsAttachment');
		
		$form['form']['lines']['form_start']['parameters']['params']['type'] = 'file';
		
		// init
		$attachments = array();
		
		// javascript
		$attachments['atch_js'] = $view->Html->script(Router::url('/', true) . 'lil_blogs_attachments/js/lil_blogs_attachments.js');
		
		$attachments['atch_panel_start'] = sprintf('<fieldset><legend>%s</legend>', __d('lil_blogs_attachments', 'Attachments', true));
		$attachments['atch_delete'] = array(
			'class' => 'form',
			'method' => 'input',
			'parameters' => array(
				'field'   => 'LilBlogs.lil_attachments_delete',
				'options' => array('type' => 'hidden')
			)
		);
		
		// upload, add and edit attachments
		$attachments['atch_table_start'] = '<table class="index" cellspacing="0" cellpadding="0">';
		$attachments['atch_table_head'] = sprintf('<thead><tr><th class="left">%1$s</th><th class="left">%2$s</th><th class="actions">%3$s</th></tr></thead>',
			__('Title', true),
			__('File', true),
			'&nbsp;'
		);
		
		// existing attachments
		if ($atchs = $a->find('all', array('recursive' => -1, 'conditions' => array('model' => 'Post', 'foreign_id' => $view->Html->value('Post.id'))))) {
			$attachments['atch_tbody_old_start'] = '<tbody>';
			foreach ($atchs as $atch) {
				$attachments[sprintf('atch_li_%s_start', $atch['LilBlogsAttachment']['id'])] = sprintf('<tr><td>%1$s</td><td>%2$s</td><td>%3$s</td></tr>',
					$view->Html->clean($atch['LilBlogsAttachment']['title']),
					$view->Html->clean($atch['LilBlogsAttachment']['original']),
					$view->Html->link(
						$view->Html->image('/lil_blogs_attachments/img/remove.gif', array('alt' => __('Remove Item', true))),
						'javascript:void(0);',
						array(
							'onclick' => sprintf('return removeLilAttachment(this, \'%s\');', $atch['LilBlogsAttachment']['id']),
							'escape' => false
						)
					)
				);
			}
			$attachments['atch_tbody_old_end'] = '</tbody>';
		}
		
		// new attachments
		$attachments['atch_tbody_new_start'] = '<tbody>';
		$attachments['atch_li_0_start'] = sprintf('<tr><td>%1$s</td><td>%2$s</td><td>%3$s</td></tr>',
			$view->Form->input('LilBlogsAttachment.0.title', array('type' => 'text', 'label' => false, 'class' => 'lil-attachment-title')),
			
			$view->Form->input('LilBlogsAttachment.0.filename', array('type' => 'file', 'label' => false, 'class' => 'lil-attachment-filename')) . ' ' .
			$view->Form->input('LilBlogsAttachment.0.model', array('type' => 'hidden', 'value' => 'Post')),
			
			$view->Form->input('LilBlogsAttachment.0.foreign_id', array('type' => 'hidden', 'default' => $view->Html->value('Post.id'))) . ' ' .
			$view->Html->link(
				$view->Html->image('/lil_blogs_attachments/img/remove.gif', array('alt' => __('Remove Item', true))),
				'javascript:void(0);',
				array('escape' => false, 'onclick' => 'return removeLilAttachment(this);')
			)
			
		);
		$attachments['atch_tbody_new_end'] = '</tbody>';

		$attachments['atch_add'] = sprintf('<tfoot><tr><td colspan="3" class="left">%s</td></tr></tfoot>',
			$view->Html->link(__('Add an Attachment', true), 'javascript:void(0);', array('onclick' => 'return addLilAttachment(this);'))
		
		);
		$attachments['atch_table_end'] = '</table>';
		
		$attachments['atch_panel_end'] = '</fieldset>';
		
		$this->insertIntoArray($form['form']['lines'], $attachments, array('before' => 'submit'));
		
		return $form;
	}
}